package com.cy.cy2025.activity

import android.content.Context
import android.icu.text.SimpleDateFormat
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.cy.cy2025.R
import com.cy.cy2025.activity.TestAdapter.TestViewHolder
import com.cy.cy2025.databinding.ActivityTestBinding
import com.cy.cy2025.util.LogUtil
import java.util.Date


class TestActivity : AppCompatActivity() {

    val binding by lazy { ActivityTestBinding.inflate(layoutInflater) }
    val vm: TestVM by lazy { ViewModelProvider(this)[TestVM::class.java] }
    //val adapter = TestAdapter()
    val adapter2 = TestAdapter2()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        LogUtil.i("TestActivity", "TestActivity")

        binding.rv1.adapter = adapter2
        adapter2.onItemClickListener = {
            vm.removeItem(it)
        }
        vm.data.observe(this) {
            LogUtil.i("TestActivity", "data")
            adapter2.submitList(it)
        }

        vm.initData()
    }
}

data class TestItem(val number: Int, var name: String)

class TestVM : ViewModel() {
    fun initData() {
        _data.value = (0..10).map { TestItem(it, "ppa$it") }
    }

    private val _data = MutableLiveData<List<TestItem>>()
    val data: LiveData<List<TestItem>> = _data

    fun removeItem(item: TestItem) {
        val list = _data.value?.toMutableList() ?: return
        list.remove(item)
        _data.value = list
    }
}

class TestAdapter : ListAdapter<TestItem, TestViewHolder>(
    object : DiffUtil.ItemCallback<TestItem>() {
        override fun areItemsTheSame(
            oldItem: TestItem,
            newItem: TestItem
        ): Boolean {
            return oldItem.number == newItem.number
        }

        override fun areContentsTheSame(
            oldItem: TestItem,
            newItem: TestItem
        ): Boolean {
            return oldItem == newItem
        }

    }) {

    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var onItemClickListener: ((TestItem) -> Unit)? = null

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): TestViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test, parent, false)
        return TestViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: TestViewHolder,
        position: Int
    ) {
        val item = getItem(position)
        holder.tvNum.text = item.number.toString()
        holder.tvName.text = item.name
        holder.tvTime.text = format.format(Date())
        holder.itemView.setOnClickListener {
            item.name = "deleted"
            onItemClickListener?.invoke(item)
        }

    }

    inner class TestViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvNum: TextView = itemView.findViewById(R.id.item_num)
        val tvName: TextView = itemView.findViewById(R.id.item_name)
        val tvTime: TextView = itemView.findViewById(R.id.item_time)
    }

}

class TestAdapter2 : BaseQuickAdapter<TestItem, QuickViewHolder>(object : DiffUtil.ItemCallback<TestItem>() {
    override fun areItemsTheSame(
        oldItem: TestItem,
        newItem: TestItem
    ): Boolean {
        return oldItem.number == newItem.number
    }

    override fun areContentsTheSame(
        oldItem: TestItem,
        newItem: TestItem
    ): Boolean {
        return oldItem == newItem
    }

}) {

    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var onItemClickListener: ((TestItem) -> Unit)? = null

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test, parent, false)
        return QuickViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: QuickViewHolder,
        position: Int,
        item: TestItem?
    ) {
        item ?: return
        holder.setText(R.id.item_num, item.number.toString())
        holder.setText(R.id.item_name, item.name)
        holder.setText(R.id.item_time, format.format(Date()))
        holder.itemView.setOnClickListener {
            item.name = "deleted"
            onItemClickListener?.invoke(item)
        }
    }
}